class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        if (nums.size() == 1) return nums[0];
        int pre = 0;
        int ans = nums[0];
        int minx = 0;
        for (int i = 1;i < nums.size();i++) {
            pre += nums[i - 1];
            minx = min(minx, pre);
            ans = max(ans, nums[i] + pre - minx);
        }
        return ans;
    }
};


